package com.imufe.chemicalenterprisedangerwarning.controller;

import com.imufe.chemicalenterprisedangerwarning.entity.query.LoginQuery;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.RoleVO;
import com.imufe.chemicalenterprisedangerwarning.entity.vo.SafetySupervisionBureauUserDetailedVO;
import com.imufe.chemicalenterprisedangerwarning.exception.UserException;
import com.imufe.chemicalenterprisedangerwarning.service.UserRoleService;
import com.imufe.chemicalenterprisedangerwarning.service.UserService;
import com.imufe.chemicalenterprisedangerwarning.common.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author BaiBan
 * @since 2022-10-09
 */
@Api(tags = "用户")
@Log4j2
@Validated
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService service;

    @Autowired
    UserRoleService userRoleService;

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ResponseBody
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody LoginQuery login) throws UserException {
        String token = service.login(login);
        return ResponseEntity.success(token);
    }

    /**
     * 用户类型分类器, 转到对应的控制器返回不同的用户详细信息
     *
     * @return 对应用户的控制器
     */
    @ApiOperation(value = "用户详情", notes = "用户的详细信息")
    @GetMapping("/detailed")
    public String detailed() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Long userId = Long.parseLong((String) authentication.getPrincipal());

        // 获取角色
        Long roleId = userRoleService.getRoleIdByUserId(userId);
        String rolePath;
        switch (roleId.intValue()) {
            case 2 -> rolePath = "safety-supervision-bureau";
            case 4 -> rolePath = "enterprise";
            default -> rolePath = "basic";
        }
        return String.format("redirect:/user/%s/detailed?id=%d", rolePath, userId);
    }

    /**
     * 企业用户详情
     *
     * @param id 用户编号
     */
    @ApiOperation(value = "企业用户详情", hidden = true)
    @ResponseBody
    @GetMapping("/enterprise/detailed")
    public ResponseEntity<String> enterpriseUser(Long id) {
        return ResponseEntity.success("企业");
    }

    /**
     * 安监局用户详情
     *
     * @param id 用户编号
     */
    @ApiOperation(value = "安监局用户详情", hidden = true)
    @ResponseBody
    @GetMapping("/safety-supervision-bureau/detailed")
    public ResponseEntity<SafetySupervisionBureauUserDetailedVO> safetySupervisionBureauUser(Long id) {
        SafetySupervisionBureauUserDetailedVO detailedVO = service.safetySupervisionBureauUserDetailed(id);
        return ResponseEntity.success(detailedVO);
    }
}